"""
汽车加油记录相关的 Pydantic 模型
"""

from datetime import UTC, datetime

from pydantic import BaseModel, Field


class VehicleBase(BaseModel):
    """车辆基础模型"""

    name: str = Field(..., min_length=1, max_length=100, description="车辆名称")
    brand: str = Field(..., min_length=1, max_length=50, description="品牌")
    model: str = Field(..., min_length=1, max_length=50, description="型号")
    license_plate: str = Field(..., min_length=1, max_length=20, description="车牌号")
    year: int | None = Field(None, ge=1900, le=2100, description="年份")
    is_active: bool = Field(True, description="是否活跃")
    notes: str | None = Field(None, max_length=500, description="备注信息")


class VehicleCreate(VehicleBase):
    """创建车辆模型"""

    pass


class VehicleUpdate(BaseModel):
    """更新车辆模型"""

    # name字段不允许更新,因为与加油记录有关联关系
    brand: str | None = Field(None, min_length=1, max_length=50, description="品牌")
    model: str | None = Field(None, min_length=1, max_length=50, description="型号")
    license_plate: str | None = Field(
        None, min_length=1, max_length=20, description="车牌号"
    )
    year: int | None = Field(None, ge=1900, le=2100, description="年份")
    is_active: bool | None = Field(None, description="是否活跃")
    notes: str | None = Field(None, max_length=500, description="备注信息")


class VehicleResponse(VehicleBase):
    """车辆响应模型"""

    id: int
    family_id: int
    created_at: datetime
    updated_at: datetime | None = None
    family_name: str | None = None

    model_config = {"from_attributes": True}


class VehicleList(BaseModel):
    """车辆列表响应模型"""

    vehicles: list[VehicleResponse]
    total: int
    skip: int
    limit: int


class VehicleFuelRecordBase(BaseModel):
    """汽车加油记录基础模型"""

    vehicle_id: int = Field(..., gt=0, description="车辆ID")
    current_mileage: float = Field(..., gt=0, description="当前里程(公里)")
    fuel_amount: float = Field(..., gt=0, description="本次加油量(升)")
    fuel_cost: float = Field(..., gt=0, description="本次加油金额(元)")
    fuel_price: float = Field(..., gt=0, description="本次油价(元/升)")
    gas_station_type: str = Field(..., description="加油站类型")
    notes: str | None = Field(None, max_length=500, description="备注信息")
    refuel_date: datetime = Field(
        default_factory=lambda: datetime.now(UTC), description="加油日期"
    )


class VehicleFuelRecordCreate(VehicleFuelRecordBase):
    """创建汽车加油记录模型"""

    pass


class VehicleFuelRecordUpdate(BaseModel):
    """更新汽车加油记录模型"""

    vehicle_id: int | None = Field(None, gt=0, description="车辆ID")
    current_mileage: float | None = Field(None, gt=0, description="当前里程(公里)")
    fuel_amount: float | None = Field(None, gt=0, description="本次加油量(升)")
    fuel_cost: float | None = Field(None, gt=0, description="本次加油金额(元)")
    fuel_price: float | None = Field(None, gt=0, description="本次油价(元/升)")
    gas_station_type: str | None = Field(None, description="加油站类型")
    notes: str | None = Field(None, max_length=500, description="备注信息")
    refuel_date: datetime | None = Field(None, description="加油日期")


class VehicleFuelRecordResponse(VehicleFuelRecordBase):
    """汽车加油记录响应模型"""

    id: int
    family_id: int
    fuel_consumption: float | None = Field(None, description="油耗(升/100公里)")
    created_at: datetime
    updated_at: datetime | None = None
    family_name: str | None = None
    vehicle_name: str | None = None

    model_config = {"from_attributes": True}


class VehicleFuelRecordList(BaseModel):
    """汽车加油记录列表响应模型"""

    fuel_records: list[VehicleFuelRecordResponse]
    total: int
    skip: int
    limit: int


class FuelConsumptionStats(BaseModel):
    """油耗统计信息"""

    avg_fuel_consumption: float = Field(description="平均油耗(升/100公里)")
    total_fuel_cost: float = Field(description="总加油金额(元)")
    total_fuel_amount: float = Field(description="总加油量(升)")
    total_distance: float = Field(description="总行驶里程(公里)")
    record_count: int = Field(description="记录数量")
    avg_fuel_price: float = Field(description="平均油价(元/升)")


class VehicleFuelRecordWithStats(BaseModel):
    """带统计信息的加油记录响应"""

    record: VehicleFuelRecordResponse
    stats: FuelConsumptionStats | None = Field(None, description="统计信息")


class VehicleWithFuelStats(BaseModel):
    """带油耗统计的车辆响应"""

    vehicle: VehicleResponse
    latest_fuel_record: VehicleFuelRecordResponse | None = Field(
        None, description="最新加油记录"
    )
    fuel_stats: FuelConsumptionStats | None = Field(None, description="油耗统计")
